<template>
  <!-- 添加或修改库存编码替换对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">

      <el-row :gutter="20">
        <el-col :span="24">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span style="color: #0d90ef;font-size: 16px;font-weight: bold;">保留的产品</span>
            </div>
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="选择产品" prop="goodsId">
                  <el-select v-model="form.goodsId" placeholder="请选择产品" style="width: 100%;"
                             remote reserve-keyword :remote-method="searchGoods" @change="selectGoods" filterable
                             clearable>
                    <el-option v-for="item in goodsList"
                               :key="item.id" :label="item.label" :value="item.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="产品编码" prop="goodsCode">
                  <el-input v-model="form.goodsCode" placeholder="请输入产品编码" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="产品名称" prop="goodsName">
                  <el-input v-model="form.goodsName" placeholder="请输入产品名称" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="产品型号" prop="goodsModel">
                  <el-input v-model="form.goodsModel" placeholder="请输入产品型号" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-card>
        </el-col>

        <el-col :span="24" style="padding-top: 20px;">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span style="color: red;font-size: 16px;font-weight: bold;">被替换的产品</span>
            </div>
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="选择被替换产品" prop="replacedGoodsId">
                  <el-select v-model="form.replacedGoodsId" placeholder="请选被替换择产品" style="width: 100%;"
                             remote reserve-keyword :remote-method="searchReplacedGoods" @change="selectReplacedGoods"
                             filterable clearable>
                    <el-option v-for="item in goodsReplacedList"
                               :key="item.id" :label="item.label" :value="item.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="被替换编码" prop="replacedGoodsCode">
                  <el-input v-model="form.replacedGoodsCode" placeholder="请输入被替换的产品编码" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="被替换名称" prop="replacedGoodsName">
                  <el-input v-model="form.replacedGoodsName" placeholder="请输入被替换的产品名称" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="被替换型号" prop="replacedGoodsModel">
                  <el-input v-model="form.replacedGoodsModel" placeholder="请输入被替换的产品型号" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-card>
        </el-col>

      </el-row>


    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "GoodsReplaceForm",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [{required: true, message: "请选择产品", trigger: "blur"}],
        replacedGoodsId: [{required: true, message: "请选择被替换的产品", trigger: "blur"}],
      },
      url: {
        getById: '/goodsReplace/goodsReplace/',
        add: '/goodsReplace/goodsReplace/add',
        edit: '/goodsReplace/goodsReplace/edit',
      },
      goodsList: [],
      goodsReplacedList: [],
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        replacedGoodsId: null,
        replacedGoodsCode: null,
        replacedGoodsName: null,
        replacedGoodsModel: null,
        replacedGoodsCount: null,
        replacedGoodsDun: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.goodsList= [];
      this.goodsReplacedList=[];
      this.open = true;
      this.title = "添加库存编码替换";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.searchGoods(response.data.goodsName);
        this.searchReplacedGoods(response.data.replacedGoodsName);
        this.open = true;
        this.title = "修改库存编码替换";
      });
    },
    /** 提交按钮 */
    submitForm() {

      // 替换和被替换的不能为同一个产品
      if (this.form.goodsId === this.form.replacedGoodsId) {
        this.$modal.msgError("替换和被替换的产品不能是同一个产品");
        return;
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");

              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    searchGoods(query) {
      if (query !== '') {
        this.loading = true; // 开始加载数据
        let params = {"queryLabel": query, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsList = response.rows;
          for (let i = 0; i < this.goodsList.length; i++) {
            this.goodsList[i].label = this.goodsList[i].name + '-' + this.goodsList[i].model + '-' + this.goodsList[i].code;
          }
        })
      } else {
        this.goodsList = [];
      }
    },

    searchReplacedGoods(query) {
      if (query !== '') {
        this.loading = true; // 开始加载数据
        let params = {"queryLabel": query, "pageNum": 1, "pageSize": 50,};
        getAction('/goodsOrderIn/goodsOrderIn/list', params).then(response => {
          this.goodsReplacedList = response.rows;
          for (let i = 0; i < this.goodsReplacedList.length; i++) {
            this.goodsReplacedList[i].label = this.goodsReplacedList[i].name + '-' + this.goodsReplacedList[i].model + '-' + this.goodsReplacedList[i].code;
          }
        })
      } else {
        this.goodsReplacedList = [];
      }
    },


    selectGoods(value) {
      for (let i = 0; i < this.goodsList.length; i++) {
        if (this.goodsList[i].id == value) {
          this.form.goodsId = this.goodsList[i].id;
          this.form.goodsCode = this.goodsList[i].code;
          this.form.goodsName = this.goodsList[i].name;
          this.form.goodsModel = this.goodsList[i].model;
          this.$forceUpdate();
        }
      }
    },


    selectReplacedGoods(value) {
      for (let i = 0; i < this.goodsReplacedList.length; i++) {
        if (this.goodsReplacedList[i].id == value) {
          this.form.replacedGoodsId = this.goodsReplacedList[i].id;
          this.form.replacedGoodsCode = this.goodsReplacedList[i].code;
          this.form.replacedGoodsName = this.goodsReplacedList[i].name;
          this.form.replacedGoodsModel = this.goodsReplacedList[i].model;
          this.form.replacedGoodsModel = this.goodsReplacedList[i].model;
          this.form.replacedGoodsCount = this.goodsReplacedList[i].count;
          this.form.replacedGoodsRemainderCount = this.goodsReplacedList[i].remainderCount;
          this.$forceUpdate();
        }
      }
    },


  }
};
</script>
